var year = new Date().getFullYear();
var moth = new Date().getMonth() +1;

document.querySelector("#year").innerHTML = year;
document.querySelector("#moth").innerHTML = moth;
//判断闰年还是平年。
function runNian(a){
    if (a % 400 === 0 || (a % 4 === 0 && a % 100 !== 0)){
        return true;
    }else {
        return false;
    }
}

//给总天数设置一个变
var b = 0;

//算从1980年开始到2018年的总天数
for (c = 1980;c <year;c++){
    if (runNian(c)){
        b += 366;
    }else {
        b += 365;
    }
}
console.log(b);

//算本年的1月到4月的总天数

for (var d = 1;d < moth;d++){
    switch (d){
        case 1:
            b += 31;
            break;
        case 2:
            if (runNian(year)){
                b += 29;
            }else {
                b += 28;
            }
            break;
        case 3:
            b += 31;
            break;
        case 4:
            b += 30;
            break;
        case 5:
            b += 31;
            break;
        case 6:
            b += 30;
            break;
        case 7:
            b += 31;
            break;
        case 8:
            b += 31;
            break;
        case 9:
            b += 30;
            break;
        case 10:
            b += 31;
            break;
        case 11:
            b += 30;
            break;
        case 12:
            b += 31;
            break;
    }
}
console.log(b);

b += 2;
console.log(b);

//取余
var e = b % 7;
console.log(e);

if(e == 0){
    e += 7;
}

//本年的上一月的总天数
var f = 0;
if(moth = 1){
    f += 31;
}else {
    switch (moth-1){
        case 1:
            f += 31;
            break;
        case 2:
            if (runNian(year)){
                f += 29;
            }else {
                f += 28;
            }
            break;
        case 3:
            f += 31;
            break;
        case 4:
            f += 30;
            break;
        case 5:
            f += 31;
            break;
        case 6:
            f += 30;
            break;
        case 7:
            f += 31;
            break;
        case 8:
            f += 31;
            break;
        case 9:
            f += 30;
            break;
        case 10:
            f += 31;
            break;
        case 11:
            f += 30;
            break;
        case 12:
            f += 31;
            break;
    }
}
console.log(f);

//获取当前现下的时间
var thisday = new Date().getDate();

//给在html中id为dwh起一个变量
var day = document.querySelector("#dwh");

//引入上个月在本月所占的日期
for (var g = f - e +1;g <= f;g++){
    day.innerHTML += "<span class='d'>"+ g + "</span>"
}

//本月共有多少天
var h = 0;
switch (moth){
    case 1:
        h += 31;
        break;
    case 2:
        if (runNian(year)){
            h += 29;
        }else {
            h += 28;
        }
        break;
    case 3:
        h += 31;
        break;
    case 4:
        h += 30;
        break;
    case 5:
        h += 31;
        break;
    case 6:
        h += 30;
        break;
    case 7:
        h += 31;
        break;
    case 8:
        h += 31;
        break;
    case 9:
        h += 30;
        break;
    case 10:
        h += 31;
        break;
    case 11:
        h += 30;
        break;
    case 12:
        h += 31;
        break;
}

console.log(h);

//引入本月的天数
for (var i = 1; i <= h;i++){
    if (i == thisday){
        day.innerHTML += "<span class='today'>"+ thisday +"</span>"
    }else {
        day.innerHTML +="<span>"+ i + "</span>"
    }
}

//本页日历一共能放多少天
var j = 42;

//引入下个月的天数
for (var k = 1;k <= j - e - h;k++){
    day.innerHTML += "<span class='d'>"+ k +"</span>"
}